const env=require("@/common/env")
const util=require("@/utils/util")

export class Request{

    config={
        baseURI:'',
        timeout:6000,
        interceptors:{
            beforeRequest:(options)=>{
                options.header=options.header||{}
                options.header['acc_token']='admin'
                return options
            },
            success: (res) => {
                return Promise.resolve(res.data) // 请求成功返回的数据
              },
            fail:(err) => {
                const errdata=err.data||{}
                const errres=errdata.msg?errdata:{msg:'请求出错'}
                wx.showToast({
                  title: errres.msg,
                  duration:1000,//显示时长
                  mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                  icon:'error', //图标，支持"success"、"loading"  
                })
               
                return  Promise.reject(errres) // 请求失败返回的消息
              }
        }
    }

    contentType={
        json:{
            value:'json',
            header:'application/json'
        },
        formdata:{
            value:'formdata',
            header:'application/x-www-form-urlencoded'
        },
        multipart_formdata:{
            value:"multipart_formdata",
            header:'multipart/form-data'
        }
    }
    constructor(config){
       this.config.baseURI=config.baseURI
       this.config.interceptors.beforeRequest=config.beforeRequest||this.config.interceptors.beforeRequest
       this.config.interceptors.success=config.success||this.config.interceptors.success
       this.config.interceptors.fail=config.fail||this.config.interceptors.fail
       this.config.timeout=config.timeout||this.config.timeout
    }

    static create(config){
        return new Request(config)
    }

    request({
        url,
        data = {},
        header,
        method = "get",
        contentDataType='json',
        loadingText="正在请求请稍后"}){
                wx.showLoading({
                    title: loadingText,
                    mask:true
                  })
        
            // 3 Promise 
            return new Promise((resolve, reject) => {
            
            const options=this.config.interceptors.beforeRequest({
                url,
                data,
                method,
                timeout:this.config.timeout,
                header,
                contentDataType
            })
            const contentType = this.contentType[contentDataType]

            options.header['content-type']=contentType.header

              wx.request({
                url: util.combineURLs(this.config.baseURI,options.url),
                data:options.data,
                method:options.method,
                timeout: options.timeout,
                header:options.header,
                success:(res)=>{
                    res.statusCode!=200?reject(res):resolve(res)
                },
                fail: reject,
                complete() {
                        wx.hideLoading()
                }
              })
            })
            .then(this.config.interceptors.success)
            .catch(this.config.interceptors.fail)
    }

    get(options){
        options.method='get'
        return this.request(options)
    }

    post(options){
        options.method='post'
        return this.request(options)
    }

    put(options){
        options.method='put'
        return this.request(options)
    }

    delete(options){
        options.method='delete'
        return this.request(options)
    }

    upload(options){
        options.method='post'
        return this.request(options)
    }
}

const config={
    baseURI:env.config.APP_BASE_URL
}


export default Request.create(config)